﻿using CustomerManager.Core.Resources;
namespace CustomerManager.Core {
    
    
    public partial class Customers {
        partial class CustomersDataTable
        {
            public static readonly string COLUMN_EMAIL = "Email";
            public static readonly string COLUMN_LAST_NAME = "LastName";
            public static readonly string COLUMN_FIRST_NAME = "FirstName";

            private void CheckEmail(CustomersRow row, object value)
            {
                string v = (value == null ? null : value.ToString());
                row.SetColumnError(COLUMN_EMAIL, (Tools.IsEmail(v) ? "" : Strings.NotValidFormat));
            }

            private void CheckLastName(CustomersRow row, object value)
            {
                string v = (value == null ? null : value.ToString());
                row.SetColumnError(COLUMN_LAST_NAME, (!string.IsNullOrEmpty(v) ? "" : Strings.Required));
            }

            private void CheckFirstName(CustomersRow row, object value)
            {
                string v = (value == null ? null : value.ToString());
                row.SetColumnError(COLUMN_FIRST_NAME, (!string.IsNullOrEmpty(v) ? "" : Strings.Required));
            }

            protected override void OnColumnChanged(System.Data.DataColumnChangeEventArgs e)
            {
                if (e.Column.ColumnName == COLUMN_EMAIL)
                    CheckEmail((CustomersRow)e.Row, e.ProposedValue);
                else if (e.Column.ColumnName == COLUMN_FIRST_NAME)
                    CheckFirstName((CustomersRow)e.Row, e.ProposedValue);
                else if (e.Column.ColumnName == COLUMN_LAST_NAME)
                    CheckLastName((CustomersRow)e.Row, e.ProposedValue);
                base.OnColumnChanged(e);
            }

            protected override void OnTableNewRow(System.Data.DataTableNewRowEventArgs e)
            {
                CheckEmail((CustomersRow)e.Row, null);
                CheckFirstName((CustomersRow)e.Row, null);
                CheckLastName((CustomersRow)e.Row, null);
                base.OnTableNewRow(e);
            }
        }
    }
}
